﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WorkFlowSystem.DataBase;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;
using WorkFlowSystem.Common;

namespace WorkFlowSystem.Business
{
    class BIZUserInfo
    {
        private string get_sql(string department)
        {
            string sql = "";
            if (department == "所有部门")
            {
                sql = "select distinct ui.uid, ui.u_name, ui.u_sex, ul.u_auth, d.d_name, p.p_name from user_info as ui,user_login as ul,department as d, position_info as p " +
                    "where d.did=ui.u_did and ul.uid=ui.uid and p.pid=ui.u_pid";
            }
            else
            {
                sql = "select distinct ui.uid, ui.u_name, ui.u_sex, ul.u_auth, d.d_name, p.p_name from user_info as ui,user_login as ul,department as d, position_info as p " +
                    "where d.d_name=@dpt_name and d.did=ui.u_did and ul.uid=ui.uid and p.pid=ui.u_pid";
            }
            return sql;
        }

        public void getUserInfoData(bool isInitFlag, string dpt_name, ref DataTable dt, ref int dataCnt)
        {
            if (isInitFlag)
            {
                //初始化时显示15条空记录
                for (int i = 0; i < 15; i++)
                {
                    dt.Rows.Add(dt.NewRow());
                }
            }
            else
            {
                DBOperator db_op = new DBOperator();
                if (db_op.ConnectDB() < 0)
                {
                    MessageBox.Show("无法连接服务器，请检查网络连接");
                }
                else
                {

                    List<SqlParameter> para_list = new List<SqlParameter>();
                    string sql = get_sql(dpt_name);
                    para_list.Add(new SqlParameter("@dpt_name", dpt_name));
                    if (db_op.ExecuteDataTable(sql, para_list, false, ref dt) < 0)
                    {
                        Logger.WriteLogError("get user info fail");
                    }
                    db_op.CloseDB();

                    if (dt.Rows.Count <= 0)
                    {
                        MessageBox.Show("没有员工记录");
                        Logger.WriteLog("no user has been created yet");
                        dt.Clear();
                    }
                }
                dataCnt = dt.Rows.Count;
                int dtRowCount = dt.Rows.Count;
                //查询数据如果不满15条，补充数据到15条。
                if (dtRowCount < 15)
                {
                    for (int i = 0; i < 15 - dtRowCount; i++)
                    {
                        dt.Rows.Add(dt.NewRow());
                    }
                }
            }
            return;
        }
    }
}
